<?php
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';
require_once 'modules/default/controllers/PacienteController.php';
require_once 'Zend/Controller/Action.php';
require_once( 'Zend/Db.php' );
require_once( 'Zend/Db/Table.php' );
require_once( 'Zend/Db/Table/Abstract.php' );
require_once( 'models/Dias_De_Trabajo.php');
require_once( 'models/Consulta.php');



class IndexController extends Zend_Controller_Action{
    	

    public function indexAction()
    {
        $this->_forward('process');
    }
	
	   
	public function logoutAction() {
    	Zend_Auth::getInstance()->clearIdentity();
    	$this->_helper->redirector('index', 'index');
    }
    
    
    private function getLoginForm() {
		$form = new Zend_Form();
		$form->setAction("/index.php/index/process")->setMethod('post');
		
		
		// Create and configure username element:
		$username = $form->createElement('text', 'username', array('label' => 'Usuario'));
		$username->addValidator('alnum')
		         ->addValidator('regex', false, array('/^[a-z]+/'))
		         ->addValidator('stringLength', false, array(4, 20))
		         ->setRequired(true)
		         ->addFilter('StringToLower');
		
		// Create and configure password element:
		$password = $form->createElement('password', 'password', array('label' => 'Clave'));
		$password->addValidator('StringLength', false, array(4))
		         ->setRequired(true);
		
		// Add elements to form:
		$form->addElement($username)
		     ->addElement($password)
		     ->addElement('hidden', 'loginTrack', array('values' => 'logPost'))
		     ->addElement('submit', 'login', array('label' => 'Ingresar'));
    	return $form;
    }
    
	public function processAction()
    {
        $request = $this->getRequest();

        // Check if we have a POST request
        if ($request->isPost()) {
            
        

        	// Get our form and validate it
        	$form = $this->getLoginForm();
        	if (!$form->isValid($request->getPost())) {
        	    // Invalid entries
        	    $this->view->form = $form;
        	    return $this->render('index'); // re-render the login form
        	}
	
	        // Get our authentication adapter and check credentials
	        $adapter = $this->getAuthAdapter($form->getValues());
	        $auth    = Zend_Auth::getInstance();
	        
        
			$adapter->setIdentity($_POST['username']);
			$adapter->setCredential($_POST['password']);
		
        
       		$result  = $auth->authenticate($adapter);
        	if (!$result->isValid()) {
        	    // Invalid credentials
        	    $form->setDescription('Invalid credentials provided');
        	    $this->view->form = $form;
        	     $this->view->message = 'Nombre de usuario o contraseña no válidos';
        	    return $this->render('index'); // re-render the login form
        	}

        	// We're authenticated! Redirect to the home page
        	$this->_helper->redirector('index', 'welcome');
    	}
    	else
    	{
    		$form = $this->getLoginForm();
    	   	$this->view->form = $form;
    	   	
    	   	return $this->render('index'); // re-render the login form
    	}
    }
    
    
	protected function getAuthAdapter()
    {
    	$config = Zend_Registry::getInstance()->configuration;
        $params = $config->db->toArray();
        $dbAdapter = Zend_Db::factory($config->db->adapter, $params);
        $authAdapter =  new Zend_Auth_Adapter_DbTable($dbAdapter);
        $authAdapter->setTableName($config->auth->tableName)
                    ->setIdentityColumn($config->auth->identityColumn)
                    ->setCredentialColumn($config->auth->credentialColumn);
        return $authAdapter;
    }
    
}   
